<template>
  <a-drawer :title="title" :width="width" placement="right" :closable="false" @close="close" :visible="visible">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="isShow" label="员工姓名" :disabled="true" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name']" placeholder="请输入员工姓名"></a-input>
        </a-form-item>
        <a-form-item v-show="!isShow" label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-search-select-tag
            :disabled="disableSubmit"
            type="list"
            v-decorator="['partyId', validatorRules.partyId]"
            :trigger-change="true"
            @change="chooseUser"
            :dict="'Person,name,partyId,isModule=HR'"
            placeholder="请选择员工姓名"
          />
        </a-form-item>
        <a-form-item label="登录账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['mobile', validatorRules.mobile]" placeholder="请输入登录账号"></a-input>
        </a-form-item>

        <a-form-item label="所属BU" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag v-decorator="['buCode', validatorRules.buCode]"
                             :trigger-change="true"  dictUrl="listAllBu"/>
        </a-form-item>

      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk" v-if="!disableSubmit">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import validate from '@/utils/validate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'PerTemporaryTaskModal',
  components: {
    JDictSelectTag,
    JSearchSelectTag,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      dateFormat: 'YYYY-MM-DD',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        partyId: {
          rules: [{ required: true, message: '请选择员工姓名!', trigger: 'blur' }],
        },
        mobile: {
          rules: [{ required: true, message: '请输入登录账号!' }],
        },
        buCode: {
          rules: [{ required: true, message: '请选择所属BU!' }],
        },
      },
      url: {
        add: '/online/user/createCrmUserLogin',
        getHrEmployee: '/online/user/getHrEmployee',
      },
      isShow:false,
      disableSubmit: false,
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record);
      if(record.partyId){
          this.isShow = true;
          if(!record.mobile)record.mobile = record.phone
      }else{
        this.isShow = false;
      }

      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name','partyId', 'mobile','buCode'))
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleChange() {},
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = this.url.add
          let method = 'post'
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(this.model, 'name','partyId', 'mobile','buCode'))
    },
    chooseUser(e) {
      httpAction(this.url.getHrEmployee, { partyId: e }).then((res) => {
        if (res.result.phone) {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              mobile: res.result.phone,
            })
          })
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 30px;
  margin-bottom: 30px;
  float: right;
}
</style>
